#include "showres.h"

ShowRes::ShowRes(QWidget *parent, QMap<QString, QString> *map) : QWidget(parent)
{
    QVBoxLayout *vLayout = new QVBoxLayout();
    QHBoxLayout *hLayout1 = new QHBoxLayout();
    hLayout1->setMargin(0);
    QWidget *widget1 = new QWidget();
    QHBoxLayout *hLayout2 = new QHBoxLayout();
    hLayout2->setMargin(0);
    QWidget *widget2 = new QWidget();
    hLayout1->setMargin(0);

    // 创建控件
    labelFileTitle = new QLabel((*map)["file_title"]);
    labelFileTime = new QLabel((*map)["file_time"]);
    labelFileSize = new QLabel((*map)["file_size"]);
    labelUserName = new QLabel((*map)["file_user"]);
    labelUserName->installEventFilter(this);
    labelFileName = new QLabel((*map)["file_name"]);
    labelFilePay = new QLabel((*map)["file_pay"]);
    labelFileType = new QLabel((*map)["file_type"]);
    btnDownload = new QPushButton("下载");
    connect(btnDownload, SIGNAL(clicked()), this, SLOT(slotDownloadRes()));
    // 第一行 标题
    QFont font;
    font.setBold(true);
    font.setUnderline(true);
    labelFileTitle->setFont(font);
    vLayout->addWidget(labelFileTitle);

    // 第二行，文件名字
    QLabel *labelPix = new QLabel();
    QPixmap pix(":/res/download.jpeg");
    QPixmap dst = pix.scaled(50,50,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    labelPix->setPixmap(dst);
    hLayout1->addWidget(labelPix);
    hLayout1->addWidget(labelFileName);
    hLayout1->addWidget(labelFilePay);
    QSpacerItem *spacer1 = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Ignored);
    hLayout1->addItem(spacer1);
    widget1->setLayout(hLayout1);
    vLayout->addWidget(widget1);

    // 第三行，其他信息
    QLabel *label1 = new QLabel("作者:");
    label1->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    hLayout2->addWidget(label1);
    hLayout2->addWidget(labelUserName);
    QLabel *label2 = new QLabel("上传时间:");
    label2->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    hLayout2->addWidget(label2);
    hLayout2->addWidget(labelFileTime);
    QLabel *label3 = new QLabel("文件大小:");
    label3->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    hLayout2->addWidget(label3);
    hLayout2->addWidget(labelFileSize);
    QLabel *label4 = new QLabel("文件领域:");
    label4->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    hLayout2->addWidget(label4);
    hLayout2->addWidget(labelFileType);
    QLabel *label5 = new QLabel("是否收费:");
    label5->setStyleSheet("QLabel{background-color:rgb(23,243,247);}");
    hLayout2->addWidget(label5);
    hLayout2->addWidget(labelFilePay);
    QSpacerItem *spacer2 = new QSpacerItem(20, 40, QSizePolicy::Expanding, QSizePolicy::Ignored);
    hLayout2->addItem(spacer2);
    hLayout2->addWidget(btnDownload);
    widget2->setLayout(hLayout2);
    vLayout->addWidget(widget2);

    QSpacerItem *spacer3 = new QSpacerItem(20, 40, QSizePolicy::Ignored, QSizePolicy::Expanding);
    vLayout->addItem(spacer3);

    setLayout(vLayout);
    mapInfo = *map;
}

void ShowRes::slotDownloadRes()
{
    qDebug() << "res emit";
    // 发送数据到上层
    emit signalDownloadRes(mapInfo);
}

bool ShowRes::eventFilter(QObject *obj, QEvent *event)
{

    // 鼠标进入
    if(event->type() == QEvent::Enter)
    {
        labelUserName->setStyleSheet("QLabel{background-color:rgb(0, 250, 102);}");
    }
    // 鼠标离开
    else if (event->type() == QEvent::Leave)
    {
        labelUserName->setStyleSheet("QLabel{background-color:rgb(237,243,247);}");
    }
    // 鼠标单击事件
    else if(event->type() == QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        // 左键单击
        if(mouseEvent->button() == Qt::LeftButton)
        {
            // 发射信号
            emit signalUserRes(labelUserName->text());
            return true;
        }
        else
        {
            return false;
        }
    }

    return QObject::eventFilter(obj, event);
}
